<?php
require_once 'protected/extensions/paypal/CallerService.php';
class PayPal extends PaymentAPI{
	
	const API_NAME = 'PayPal';
	
	const STEP_SETEXPRESSCHECKOUT 			= 'SetExpressCheckout';
	const STEP_GETEXPRESSCHECKOUTDETAILS	= 'GetExpressCheckoutDetails';
	const STEP_DOEXPRESSCHECKOUTPAYMENT		= 'DoExpressCheckoutPayment';
		
	private $_itemNumber = 0;
	
	private $_paypalURLRequest;
	
	private $_currentStep;
	
	private $_paypalInfo = array();
	
	private $_processData;
	
	public  function apiInit(){
		$this->setApiName(self::API_NAME);
		$this->_paypalURLRequest = array(
			'HDRIMG'			=>	Yii::app()->request->hostInfo.'/images/logo.png',
			'PAYMENTACTION'		=>	'Sale',
			'CURRENCYCODE'		=>	'USD',
			'INVNUM'			=>	$this->getPaymentCode(),
			'CUSTOM'			=>	$this->getPaymentCode(),
			'AMT'				=>	0,
			'ITEMAMT'			=>	0,
			'MAXAMT'			=>	0,
			'SHIPPINGAMT'		=>	0,
			'DESCRIPTION0'		=>	'dibzees',
// 			'RETURNURL'			=>	Yii::app()->request->hostinfo.'/payment/paypal/paymentCode/'.$this->getPaymentCode(),
// 			'CANCELURL'			=>	Yii::app()->request->hostinfo.'/payment/paypal/paymentCode/'.$this->getPaymentCode(),
			'RETURNURL'			=>	'http://localhost/dibzees/05.develop/01.frontend/payment/paypal/paymentCode/'.$this->getPaymentCode(),
			'CANCELURL'			=>	'http://localhost/dibzees/05.develop/01.frontend/payment/paypal/paymentCode/'.$this->getPaymentCode(),
		);
	}
	
	public function apiRequest($step){
		$this->_currentStep = $step;
		
		if (method_exists($this,$step)){
			$this->$step();
		}
		return $this;
	}
	
	public function apiResponse($response = null){
		$response =  $this->getResponse();
		$this->paymentLog($response);
		if($this->apiResponseVaild()){
			$this->paymentLog($this->_paymentData->toArray());
		}
		return array_merge($this->_processData,$response);
	}	
	public function apiPaymentVaild(){
		return ($this->_currentStep == self::STEP_DOEXPRESSCHECKOUTPAYMENT) && ($this->_response["ACK"] == 'Success');	
	}
	
	public function apiResponseVaild(){
		return ($this->_response["ACK"] == 'Success');
	}
	
	
	//pay pal
	
	public function addItem($itemData){
		$itemTemp = array(
			'L_NAME'.$this->_itemNumber 	=>	$itemData['title'],
			//'L_NUMBER'.$this->_itemNumber	=>	$itemData['id'],
			'L_QTY'.$this->_itemNumber		=>	'1',
			'L_TAXAMT'.$this->_itemNumber	=>	'0',
			'L_AMT'.$this->_itemNumber		=>	(string)$itemData['price'],
			'L_SHIPPINGOPTIONAMOUNT'.$this->_itemNumber=>  (string)$itemData['SHIPPINGAMT'],
			'L_SHIPPINGOPTIONLABEL'.$this->_itemNumber=>'UPS',
			'L_SHIPPINGOPTIONNAME'.$this->_itemNumber=>'UPS',
			'L_SHIPPINGOPTIONISDEFAULT'.$this->_itemNumber=>'true',
		);
				
		$this->setUrlRequestValue($itemTemp);
		
		$this->_paypalURLRequest['AMT'] 	+= (string)($itemData['price']+$itemData['SHIPPINGAMT']);
		$this->_paypalURLRequest['ITEMAMT'] += (string)$itemData['price'];
		$this->_paypalURLRequest['MAXAMT'] += (string)($itemData['price']+$itemData['SHIPPINGAMT']+25.00);
		$this->_paypalURLRequest['SHIPPINGAMT'] += $itemData['SHIPPINGAMT'];
		$this->_itemNumber++;
		return $this;
	}
	
	public function setUrlRequestValue($requestValue){		
		$this->_paypalURLRequest = array_merge($this->_paypalURLRequest,$requestValue);		
		return $this;
	}
	
	public function step($step,$nvpStr){		
		$this->_response = hash_call($step, $nvpStr);
		$this->_response['STEP'] = $step;
		return $this->_response;
	}
	
	public function SetExpressCheckout(){
		
		$this->paymentLog(' paypal Start ');
			
		$this->paymentLog(' request : '.json_encode($this->_paypalURLRequest));
		
		$this->step($this->_currentStep,'&'.http_build_query($this->_paypalURLRequest));
		
		$this->paymentLog(' response : '.json_encode($this->_response));	
		
		if ($this->apiResponseVaild()){
			Yii::app()->getRequest()->redirect(PAYPAL_URL.$this->_response['TOKEN']);
		}
		return $this;
	}
	
	public function GetExpressCheckoutDetails(){		
		$token = $this->_request['token'];
		$this->step($this->_currentStep,'&TOKEN='.$token);
		
		$this->paymentLog(' getExepress Data:'.json_encode($this->_response));
		if ($this->apiResponseVaild()){
			$this->_processData = $this->getResponse();
			//$this->apiRequest(self::STEP_DOEXPRESSCHECKOUTPAYMENT);
		}
		return $this;
	}
	
	public function DoExpressCheckoutPayment(){
		$tempDoExpressCheckoutUrl = array(
			'TOKEN'			=>	$this->_response['TOKEN'],
			'INVNUM'		=>	$this->_response['INVNUM'],
			'CUSTOM'		=>	$this->_response['CUSTOM'],
			'PAYERID'		=>	$this->_response['PAYERID'],
			'PAYMENTACTION'	=>	'Sale',
			'AMT'			=>	$this->_response['AMT'],
			'CURRENCYCODE'	=>	$this->_response['CURRENCYCODE'],
			'IPADDRESS'		=>	$_SERVER['SERVER_NAME'],
// 			'SHIPPINGAMT'	=>  $this->_response['SHIPPINGAMT'],
		);	
		$this->paymentLog(' checkout data:'.json_encode($tempDoExpressCheckoutUrl));
		$this->step($this->_currentStep,'&'.http_build_query($tempDoExpressCheckoutUrl));
		$this->paymentLog(' checkout data:'.json_encode($this->_response));
		return $this;
	}	
}